import Vue from 'vue'

import Vuex from 'vuex'

import { INCREMENT } from './mutationTypes'

Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    count: 0,
    show: true,
    a: {
      b: {
        x: 0
      }
    }
  },

  mutations: {
    // 纯函数
    // 1、给定的输入，一定会得到确定的出入
    // 2、一定不要执行副作用
    [INCREMENT](state, payload) {
      // setTimeout(() => {
      //   state.count++
      // }, 1000)
      state.count += payload
    },
    decrement(state, data) {
      state.count -= data
    },

    setShow(state) {
      state.show = !state.show
    }
  },

  // store里的计算属性
  getters: {
    doubleCount(state) {
      return (num) => {
        return state.count * num
      }
    },
    tribbleCount(state) {
      return (num) => {
        return state.count * num
      }
    }
  },

  actions: {
    [INCREMENT]({commit}, data) {
      // setTimeout(() => {
      //   commit('increment')
      // }, 1000)
      commit(INCREMENT, data)

    },
    decrement({commit}) {
      commit('decrement')
    },

    changeShow({commit}) {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          commit('setShow')
          resolve()
        }, 1000)
      })
    }
  }
})

// let x = 'a'

// let y = {
//   [x]: 0,
//   b: 1
// }

// console.log(y.a)

export default store